package com.xiashitech.apiservice.service.impl;

import com.xiashitech.apiservice.dao.DesenseRuleMapper;
import com.xiashitech.apiservice.dao.model.DesenseRule;
import com.xiashitech.apiservice.dto.DesenseRuleDTO;
import com.xiashitech.apiservice.service.IDesenseRule;
import com.xiashitech.apiservice.util.BeanCopierUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor =@_(@Autowired))
public class DesenseRuleServiceImpl implements IDesenseRule {

    private final DesenseRuleMapper desenseRuleMapper;

    public void createDesenseRule(DesenseRuleDTO desenseRuleDTO) {
        DesenseRule desenseRule = BeanCopierUtil.copy(desenseRuleDTO, DesenseRule.class);
        desenseRuleMapper.insertDesenseRule(desenseRule);
    }

    public void updateDesenseRule(DesenseRuleDTO desenseRuleDTO) {
        DesenseRule desenseRule = BeanCopierUtil.copy(desenseRuleDTO, DesenseRule.class);
        desenseRuleMapper.updateDesenseRule(desenseRule);
    }

    public void deleteDesenseRule(DesenseRuleDTO desenseRuleDTO) {
        DesenseRule desenseRule = BeanCopierUtil.copy(desenseRuleDTO, DesenseRule.class);
        desenseRuleMapper.deleteDesenseRule(desenseRule);
    }

    public List<DesenseRuleDTO> searchDesenseRule(DesenseRuleDTO desenseRuleDTO) {
        List<DesenseRule> desenseRules = desenseRuleMapper.selectDesenseRule(BeanCopierUtil.copy(desenseRuleDTO, DesenseRule.class));
        return desenseRules.stream().map(desenseRule -> BeanCopierUtil.copy(desenseRule, DesenseRuleDTO.class)).collect(Collectors.toList());
    }
}
